package com.gitee.mazhenggg.mall.account.web.service;

import com.gitee.mazhenggg.mall.account.domain.repository.AccountRepository;
import com.gitee.mazhenggg.mall.account.domain.type.Money;
import com.gitee.mazhenggg.mall.account.domain.type.UserId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountService {
    @Autowired
    AccountRepository accountRepository;

    public Money getAccountByUserId(UserId userId){
        Money available =accountRepository.getBalanceByUserId(userId);
        return available;
    }
    @Transactional
    public Boolean consumeByUserId(UserId userId,Money amount){
//        if(( accountRepository.getBalanceByUserId(userId)-amount)<0){
//            return false;
//        }
        accountRepository.reduceAccountByUserId(userId,amount);
        return true;
    }
}
